<template>
  <div class="base-curriculum">
    <!--    第一个卡片-->
    <el-card>
      <div class="desc-box">
        <div class="title">基础课程详情列表</div>
        <div class="desc">您可以对基础课程详情列表进行时间上增删改查操作，但编辑功能不提供修改基础学科和基础课程的操作，只做展示！</div>
      </div>
      <div class="use-box">
        <div>
          <el-button class="add-button" type="primary" @click="openDialogComponent(0)">添加基础课程详情</el-button>
        </div>
        <div class="search-box">
          <div>
            <span class="search-name">基础学科 :</span>
            <el-select v-model="queryInfo.SubjectID" placeholder="请选择绑定的基础学科" @change="selectSubjectChange">
              <el-option label="-----查询全部-----" :value="0"></el-option>
              <el-option v-for="(item,index) in subjectDataList"
                         :key="index"
                         :label="item.Text"
                         :value="item.Value">
              </el-option>
            </el-select>
          </div>
          <div class="search-input">
            <span class="search-name">基础课程 :</span>
            <el-select v-model="queryInfo.CurriculumID" placeholder="请选择绑定的基础课程">
              <el-option label="-----查询全部-----" :value="0"></el-option>
              <el-option v-for="(item,index) in curriculumDataList"
                         :key="index"
                         :label="item.Text"
                         :value="item.Value">
              </el-option>
            </el-select>
          </div>
          <div>
            <el-button
              type="primary"
              class="search-button"
              @click="getBaseCurriculumDetailList()">查询
            </el-button>
          </div>
        </div>
      </div>
    </el-card>
    <!--    第二个卡片-->
    <el-card class="second-card">
      <el-table
        ref="table"
        border
        max-height="530px"
        v-loading="loading"
        element-loading-text="拼命加载中......"
        :data="baseCurriculumDetailList"
        :cell-style="{textAlign:'center'}"
        :header-cell-style="{textAlign:'center'}"
        :header-cell-class-name="setHeaderRowClassName"
      >
        <el-table-column type="index" label="序号" fixed></el-table-column>
        <el-table-column prop="SubjectName" label="基础学科名称" width="120px"></el-table-column>
        <el-table-column prop="ClassName" label="基础课程名称" width="120px"></el-table-column>
        <el-table-column prop="Week" label="上课星期"></el-table-column>
        <el-table-column prop="TimeInterval" label="上课时段"></el-table-column>
        <el-table-column prop="StartTime" label="开始时间"></el-table-column>
        <el-table-column prop="EndTime" label="结束时间"></el-table-column>
        <el-table-column prop="CreationTime" label="详情创建时间" :formatter="dateFormat" width="150px"></el-table-column>
        <el-table-column label="启停状态" width="110px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.Status"
              :active-value="1"
              :inactive-value="0"
              @change="statusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="290px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="openDialogComponent(scope.row.ID)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteCurriculumInfo(scope.row.ID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页-->
      <pagination :child-msg="queryInfo" @callFather="callFather"></pagination>

    </el-card>
    <!--  添加和编辑的对话框-->
    <add-and-edit-dialog
      v-if="isShowDialogComponent"
      :is-show-dialog-component="isShowDialogComponent"
      :title="title"
      :judge-id="judgeId"
      @handleClose="handleClose"
    ></add-and-edit-dialog>
  </div>
</template>

<script>
//引入添加编辑对话框组件
import AddAndEditDialog from './childComps/AddAndEditDialog'
//引入封装接口的类
import BaseCurriculum from '../../model/baseCurriculum'
//引入课程详情接口的类
import BaseCurriculumDetails from '../../model/baseCurriculumDetail'
//引入分页的组件
import Pagination from '../../components/base/pagination/Pagination'
import moment from 'moment'

export default {
  name: 'BaseCurriculum',
  components: {
    AddAndEditDialog,
    Pagination
  },
  data () {
    return {
      loading: false,
      title: '',
      isShowDialogComponent: false,
      judgeId: 0,
      //保存获取到的基础学科信息
      subjectDataList: [],
      //保存获取到的基础课程信息
      curriculumDataList: [],
      //课程详情列表
      baseCurriculumDetailList: [],
      //获取列表需要传入的参数
      queryInfo: {
        SubjectID: 0,
        CurriculumID: 0,
        Page: 1,
        Limit: 10,
        total: 0
      },
      statusChangeForm: {
        id: '',
        status: ''
      },
    }
  },
  created () {
    this.getBaseCurriculumDetailList()
    this.getSubjectList()
  },

  methods: {
    //时间戳
    dateFormat (row, column) {
      let date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    setHeaderRowClassName ({ rowIndex }) {
      if (rowIndex === 0) {
        return 'setHeaderSubjectColumnName'
      }
    },
    handleClose (payload) {
      if (payload) {
        this.getBaseCurriculumDetailList()
      }
      this.isShowDialogComponent = false
    },
    //分页的回调
    callFather (value) {
      if (value.currentPage) this.queryInfo.Page = value.currentPage
      if (value.pageSize) this.queryInfo.Limit = value.pageSize
      this.getBaseCurriculumDetailList()
    },
    //打开添加、编辑话框
    openDialogComponent (id) {
      this.title = id === 0 ? '添加基础课程详情' : '编辑基础课程详情'
      this.judgeId = id === 0 ? 0 : id
      this.isShowDialogComponent = true
    },
    //获取基础课程详情列表
    async getBaseCurriculumDetailList () {
      try {
        this.loading = true
        const res = await BaseCurriculumDetails.PageSearch(this.queryInfo)
        this.loading = false
        this.baseCurriculumDetailList = res.ListData
        this.queryInfo.total = res.Total
      } catch (err) {
        this.loading = false
      }
    },
    //删除操作
    async deleteCurriculumInfo (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该基础课程详情, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除该基础课程详情的操作')
      } else {
        await BaseCurriculumDetails.Delete({ ID: id })
        this.$message.success('成功删除该基础课程详情')
        await this.getBaseCurriculumDetailList()
      }
    },
    //修改启停状态
    async statusChange (rowInfo) {
      this.statusChangeForm.id = rowInfo.ID
      this.statusChangeForm.status = rowInfo.Status
      await BaseCurriculumDetails.UpdateStatus(this.statusChangeForm)
      this.$message.success('成功修改基础课程详情的启停状态')
    },

    //获取学科列表信息
    async getSubjectList () {
      const res = await BaseCurriculum.getSubjectList()
      this.subjectDataList = res
    },
    //获取课程列表详情
    async getPageSearchList () {
      const res = await BaseCurriculum.pageSearchList({ ID:  this.queryInfo.SubjectID })
      this.curriculumDataList = res
    },
    async selectSubjectChange () {
      this.queryInfo.CurriculumID=0
      if (this.queryInfo.SubjectID !== 0) {
        await this.getPageSearchList()
      }
    }
  }
}
</script>


<style scoped>
@import "../../../src/assets/style/realize/tableStyle.css";
.base-curriculum {
  background-color: #eee;
}
.el-card {
  box-shadow: none;
  margin-top: 10px;
}
.second-card {
  padding-bottom: 50px;
  position: relative;
}
.el-pagination {
  position: absolute;
  right: 20px;
  bottom: 10px;
}
/*给表头一行加背景颜色*/
/deep/ .setHeaderSubjectColumnName {
  background-color: #E6E6FA !important;
  opacity: 0.7 !important;
}
/*描述文本域明显一些的样式，设置高度*/
/deep/ .el-textarea__inner {
  height: 150px;
}

</style>


